package edu.nyu.hps.assignment3;

import java.io.*;
import java.net.*;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String response,temp;
		int port = 4242;
		int maxWeights = 0;
		int player = 0;
		int round;
		int torque1;
		int torque2;
		int time;
		int gameWeights[];
		boolean connected = false;
		int gameState[] = { 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0 };
		// Create Socket connection
		Socket client;

		InputStreamReader isr;
		BufferedReader in = null;
		PrintWriter out = null;
		port = Integer.parseInt(args[0]);
		// Initialization
		try {
			client = new Socket("localhost", port);
			System.out.println("New socket opened!");
			isr = new InputStreamReader(client.getInputStream());
			in = new BufferedReader(isr);
			out = new PrintWriter(client.getOutputStream(), true);

			while (!connected) {
				response = null;
				out.println("JOIN:Hiral");

				// Waiting for the ACK or NACK responses from the server
				while ((response = in.readLine()) == null) {
				}

				if (response.charAt(0) == 'N') {
					// this.wait(2000);

				} else if (response.charAt(0) == 'A') {
					player = response.split(":")[1].equalsIgnoreCase("0") ? 0
							: 1;
					System.out.println("Player is" + player);
					connected = true;
				}
			}
			// waiting for the start response from the server
			response = null;
			while ((response = in.readLine()) == null) {
			}

			// When the response is received to start the game
			if (response.indexOf("START") == 0) {
				maxWeights = Integer.parseInt(response.split(":")[1]);
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		NoTipping game = new NoTipping(player, maxWeights, true);

		response = null;
		while (true) {
			try {
				while ((response = in.readLine()) == null) {
				}
				if (response.indexOf("ADD") == 0) {
					String process[] = response.split(":");
					round = Integer.parseInt(process[1]);
					time = Integer.parseInt(process[2]);
					torque1 = Integer.parseInt(process[3]);
					torque2 = Integer.parseInt(process[4]);
					int i = 0;

					for (String c : process[7].split(",")) {
						gameState[i++] = Integer.parseInt(c);
					}
					game.setGamePositions(gameState);
					game.setMytorque1(torque1);
					game.setMytorque2(torque2);
					{
						System.out.println("Round: " + round);
						System.out.println("Torque 0: " + torque1);
						System.out.println("Torque 1: " + torque2);
						temp = game.process();
						System.out.println("ADD:" + temp);
						System.out.println();
						System.out.println();
					}
					out.println("ADD:" + temp);

				} else if (response.indexOf("REMOVE") == 0) {

					String process[] = response.split(":");
					round = Integer.parseInt(process[1]);
					time = Integer.parseInt(process[2]);
					torque1 = Integer.parseInt(process[3]);
					torque2 = Integer.parseInt(process[4]);

					int i = 0;

					for (String c : process[5].split(",")) {
						gameState[i++] = Integer.parseInt(c);
					}
					game.setGamePositions(gameState);
					game.setMytorque1(torque1);
					game.setMytorque2(torque2);
					game.addPhase = false;
					game.updateSetOne();
					{
						System.out.println("Round: " + round);
						System.out.println("Torque 0: " + torque1);
						System.out.println("Torque 1: " + torque2);
						temp = game.process();
						System.out.println("REMOVE:" + temp);
						System.out.println("The weight to be removed is:"
								+ game.gamePositions[Integer.parseInt(temp)+10]);
						System.out.println();
						System.out.println();
					}
					out.println("REMOVE:" + temp);
				} else if (response.indexOf("STATUS") == 0) {
					if(response.split(":")[1].equalsIgnoreCase("0"))
					{
						System.out.println("I Win! :)");
					} else {
						System.out.println("I Lose! :(");
					}
					break;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}
